package com.hdit.guimei.web;

import com.hdit.guimei.domain.GoodsComment;
import com.hdit.guimei.domain.My_comment;
import com.hdit.guimei.service.MycommentService;
import com.hdit.guimei.util.OssUtil;
import com.hdit.guimei.util.ResponseData;
import com.hdit.guimei.util.SubStringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author 冰镇小可乐
 * @ClassNameMycommentController
 * @Description TODO
 * @Date 2021/1/21 16:13
 * Version 1.0
 */
@Controller
@ResponseBody
public class MycommentController {
    @Autowired
    private MycommentService mycommentService;
    /***
    * @Method 用户添加评论
    * @Author 冰镇小可乐
    * @params  [my_comment]
    * @Return int
    * @Exception
    * @Date 2021/1/21 16:18
    */
    @RequestMapping("/mycomment/add")
    int addMycomment(My_comment my_comment, MultipartFile picture1, MultipartFile picture2, MultipartFile picture3,
                     MultipartFile picture4, MultipartFile picture5, MultipartFile picture6){
        if (picture1!=null&&!picture1.isEmpty()) {
            String picture1_url = new OssUtil().uploadDocument(picture1, new SubStringUtil().SubFileName(picture1.getOriginalFilename(), "."));
            my_comment.setCon_pic1(picture1_url);
            System.out.println(picture1_url);
        }
        if (picture2!=null&&!picture2.isEmpty()) {
            String picture2_url = new OssUtil().uploadDocument(picture2, new SubStringUtil().SubFileName(picture2.getOriginalFilename(), "."));
            my_comment.setCon_pic2(picture2_url);
            System.out.println(picture2_url);
        }
        if (picture3!=null&&!picture3.isEmpty()) {
            String picture3_url = new OssUtil().uploadDocument(picture3, new SubStringUtil().SubFileName(picture3.getOriginalFilename(), "."));
            my_comment.setCon_pic3(picture3_url);
            System.out.println(picture3_url);
        }
        if (picture4!=null&&!picture4.isEmpty()) {
            String picture4_url = new OssUtil().uploadDocument(picture4, new SubStringUtil().SubFileName(picture4.getOriginalFilename(), "."));
            my_comment.setCon_pic4(picture4_url);
            System.out.println(picture4_url);
        }
        if (picture5!=null&&!picture5.isEmpty()) {
            String picture5_url = new OssUtil().uploadDocument(picture5, new SubStringUtil().SubFileName(picture5.getOriginalFilename(), "."));
            my_comment.setCon_pic5(picture5_url);
        }
        if (picture6!=null&&!picture6.isEmpty()) {
            String picture6_url = new OssUtil().uploadDocument(picture6, new SubStringUtil().SubFileName(picture6.getOriginalFilename(), "."));
            my_comment.setCon_pic6(picture6_url);
        }
        //用户id
        System.out.println(my_comment);
        int i=mycommentService.addMycomment(my_comment);
        return i;
    }
    /***
    * @Method 删除用户评论
    * @Author 冰镇小可乐
    * @params  [id, good_id]
    * @Return int
    * @Exception
    * @Date 2021/1/21 16:17
    */
    @RequestMapping("/mycomment/delete")
    @ResponseBody
    int deleteMycomment(int id,int good_id){
        int i=mycommentService.deleteMycomment(id,good_id);
        return i;
    }
    /***
    * @Method 用户查看自己的评论
    * @Author 冰镇小可乐
    * @params  [id]
    * @Return java.util.List<com.hdit.guimei.domain.GoodsComment>
    * @Exception  
    * @Date 2021/1/21 16:52
    */
    @RequestMapping("/mycomment/findUser")
    List<GoodsComment>  findGoodsCommentByUser(int id){
        List<GoodsComment> list =mycommentService.findGoodsByUser(id);
        return list;
    }
    /***
    * @Method 商品详情页评论
    * @Author 冰镇小可乐
    * @params  [goods_id]
    * @Return java.util.List<com.hdit.guimei.domain.GoodsComment>
    * @Exception  
    * @Date 2021/1/21 16:53
    */
    @RequestMapping("/mycomment/findComment")
    @ResponseBody
    List<GoodsComment> findGoodsCommentBy( int good_id, Model model){
        System.out.println("商品评价"+good_id);
        List<GoodsComment> list=mycommentService.fingGoodsBy(good_id);
        model.addAttribute("list",list);
        return list;
    }
    @RequestMapping("/findAllGoodsComment")
    @ResponseBody
    ResponseData<List> findAllGoodsComment(){
        List<GoodsComment> comments = mycommentService.findAllGoodsComment();
        System.out.println(comments);
        ResponseData<List> res = new ResponseData<>();
        res.setCode(200);
        res.setMess("ok");
        res.setData(comments);
        return res;
    }
    @RequestMapping("/updateGoodsCommentState")
    @ResponseBody
    ResponseData<Integer> updateGoodsCommentState(int id,int con_state){
        int i = mycommentService.updateGoodsCommentState(id , con_state);
        ResponseData<Integer> res = new ResponseData<>();
        res.setCode(200);
        res.setMess("ok");
        res.setData(i);
        return res;
    }
}
